"""
@author: Sergiy Tsymbalyuk
"""

from pylons.i18n import lazy_ugettext as _

from sqlalchemy import Column, ForeignKey, Index, UniqueConstraint
from sqlalchemy.types import Integer, String, Date, Numeric, Boolean
from sqlalchemy.orm import relation

from babel.dates import get_month_names

from chowa.model.organization import Office
from chowa.model.abstract import person_full_name

from hrms.model.meta import Base
from hrms.model.person import Employee
from hrms.model.organization import Order

__all__ = ['Timesheet', 'WorkingMonth',
           'WorkingDayCode', 'WorkingDay',
           'VacationType', 'Vacation',
           'CompensatoryHoliday']

class Timesheet(Base):
    __tablename__ = 'hrms_timesheet'
    __table_args__ = (UniqueConstraint('office_id', 'year', 'month'),)
    
    id = Column(Integer, primary_key=True)
    office_id = Column(Integer, ForeignKey(Office.id), info={'verbose': _('Office')})
    year = Column(Integer, info={'verbose': _('Year')})
    month = Column(Integer, info={'verbose': _('Month')})
    
    office = relation(Office)
    
    def __unicode__(self):
        return '%s, %s %d' % (unicode(self.office),
                              get_month_names('wide', 'stand-alone')[self.month],
                              self.year)


class WorkingMonth(Base):
    __tablename__ = 'hrms_working_month'
    __table_args__ = (UniqueConstraint('employee_id', 'timesheet_id'),)
    
    id = Column(Integer, primary_key=True)
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    timesheet_id = Column(Integer, ForeignKey(Timesheet.id), info={'verbose': _('Timesheet')})
    number = Column(Integer, info={'verbose': _('Number')})
    salary = Column(Numeric(10, 2), info={'verbose': _('Salary')})
    
    employee = relation(Employee, order_by=person_full_name)
    timesheet = relation(Timesheet, backref='employees')
    
    def __unicode__(self):
        return unicode(self.employee)

    
class WorkingDayCode(Base):
    __tablename__ = 'hrms_working_day_code'
    
    id = Column(Integer, primary_key=True)
    abbreviation = Column(String(3), unique=True, info={'verbose': _('Abbreviation')})
    number = Column(Integer, unique=True, info={'verbose': _('Number')})
    description = Column(String(255), info={'verbose': _('Description')})
    
    def __unicode__(self):
        return self.abbreviation


class WorkingDay(Base):
    __tablename__ = 'hrms_working_day'
    __table_args__ = (UniqueConstraint('month_id', 'day'),)
    
    id = Column(Integer, primary_key=True)
    month_id = Column(Integer, ForeignKey(WorkingMonth.id), info={'verbose': _('Working month')})
    day = Column(Integer, info={'verbose': _('Day')})
    hours = Column(Integer, info={'verbose': _('Hours')})
    code_id = Column(Integer, ForeignKey(WorkingDayCode.id), info={'verbose': _('Code')})
    
    month = relation(WorkingMonth, backref='days')
    code = relation(WorkingDayCode)
    
    def __unicode__(self):
        return str(self.day)
    
    def __repr__(self):
        return str(self.day)


class VacationType(Base):
    __tablename__ = 'hrms_vacation_type'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), info={'verbose': _('Object|Name')})
    description = Column(String(255), info={'verbose': _('Description')})
    
    def __unicode__(self):
        return self.name


class Vacation(Base):
    __tablename__ = 'hrms_vacation'
    
    id = Column(Integer, primary_key=True)
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    days = Column(Integer)
    started = Column(Date, info={'verbose': _('Started')})
    ended = Column(Date, info={'verbose': _('Ended')})
    period_started = Column(Date, info={'verbose': _('Started')})
    period_ended = Column(Date, info={'verbose': _('Ended')})
    type_id = Column(Integer, ForeignKey(VacationType.id), info={'verbose': _('Type')})
    is_financial_aid = Column(Boolean, info={'verbose': _('Financial aid')})
    description = Column(String(255), info={'verbose': _('Description')})
    
    employee = relation(Employee)
    type = relation(VacationType)


class CompensatoryHoliday(Base):
    __tablename__ = 'hrms_compensatory_holiday'
    __table_args__ = (UniqueConstraint('employee_id', 'in_work', 'day_off'),)
    
    id = Column(Integer, primary_key=True)
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    in_work = Column(Date, info={'verbose': _('In work')})
    day_off = Column(Date, info={'verbose': _('Day off')})
    was_compensated = Column(Boolean, info={'verbose': _('Was compensated')})
    order_id = Column(Integer, ForeignKey(Order.id), info={'verbose': _('Order')})
    
    employee = relation(Employee)
    order = relation(Order)
    
    def __unicode__(self):
        return '%s >> %s' % (self.in_work, self.day_off)
